﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Collections.Generic;

namespace Crosland.Util
{
    public class EventCalendar : System.Web.UI.WebControls.Calendar
    {

        // **********************************************************
        // Gets or Sets the Name of source DataTable  
        /// <summary>
        /// If this is specified, then EventDate and EventHeader are mandatory
        /// </summary>
        public DataTable EventSource
        {
            get
            {
                if (ViewState["EventSource"] == null)
                    return null;
                else
                    return ((DataTable)ViewState["EventSource"]);
            }
            set { ViewState["EventSource"] = value; }
        }


        // Gets or sets the Date Column in the DataTable
        public string EventDate
        {
            get
            {
                if (ViewState["EventDate"] == null)
                    return string.Empty;
                else
                    return (ViewState["EventDate"].ToString());
            }
            set { ViewState["EventDate"] = value; }
        }

        // Gets or sets the Event Header Column Name in the DataTable
        public string EventHeader
        {
            get
            {
                if (ViewState["EventHeader"] == null)
                    return string.Empty;
                else
                    return (ViewState["EventHeader"].ToString());
            }
            set { ViewState["EventHeader"] = value; }
        }


        // Gets or sets the Event Description Column Name in the DataTable
        public string EventDescription
        {
            get
            {
                if (ViewState["EventDescription"] == null)
                    return string.Empty;
                else
                    return (ViewState["EventDescription"].ToString());
            }
            set { ViewState["EventDescription"] = value; }
        }

        // Gets or sets the Event Description Column Name in the DataTable
        public string CodigoActividad
        {
            get
            {
                if (ViewState["CodigoActividad"] == null)
                    return string.Empty;
                else
                    return (ViewState["CodigoActividad"].ToString());
            }
            set { ViewState["CodigoActividad"] = value; }
        }

        // Gets or sets the Event Description Column Name in the DataTable
        public bool ShowDescriptionAsToolTip
        {
            get
            {
                if (ViewState["EventDescriptionColumnName"] == null)
                    return true;
                else
                    return (Convert.ToBoolean(ViewState["ShowDescriptionAsToolTip"].ToString()));
            }
            set { ViewState["ShowDescriptionAsToolTip"] = value; }
        }
      

        public EventCalendar()
            : base()
        {
            DayRender += new DayRenderEventHandler(EventCalendarDayRender);            
     
        }

        protected void EventCalendarDayRender(object sender, DayRenderEventArgs e)
        {
            CalendarDay d = ((DayRenderEventArgs)e).Day;
            TableCell c = ((DayRenderEventArgs)e).Cell;
          
            if (this.EventSource == null)
                return;

            DataTable dt = this.EventSource;

            int i = 0;

            string[] estilos = { "calendar-activity", "calendar-active", "calendar-alert" };

            int indice = 0;
            foreach (DataRow dr in dt.Rows)
            {

                if (EventDate != string.Empty && !d.IsOtherMonth && 
                    Convert.ToDateTime(dr[this.EventDate]).ToShortDateString() == d.Date.ToShortDateString())
                {    
                    System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
                    lbl.Text = String.Format(@"
                    <div class=""content-actividad"">
                        <div class=""item-actividad"" onmouseover=""mostrarDetalle({4});"" onmouseout=""ocultarDetalle({4});"">{0}</div>
                        <div class=""ver-actividad"">
                            <div class=""rec-actividad"" id=""actividad{1}"">
                                <span>Motivo:</span> {2} <br />
                                <span>Fecha y Hora:</span> {3}
                            </div>
                        </div>                        
                    </div>", dr[EventHeader].ToString(), dr[CodigoActividad].ToString(), dr[EventDescription].ToString(),
                           dr[EventDate].ToString(), "'actividad" + dr[CodigoActividad].ToString() + "'");                    
                                        
                    c.Controls.Add(lbl);                     
                    c.CssClass = estilos[indice];
                }
                i++;
                indice++;

                if (indice == 3) indice = 0;
            }
        }

       
    }
}
